#include <iostream>
#include <string>
using namespace std;
int t;

int main(){
    
    cin >> t;
    while (t--){
        int n, k; cin >> n >> k;
        string s; cin >> s;
        int count = 0;
        int score = 0;
        for (auto& ch : s){
            if (ch == 'W'){
                count ++;
                if (count >= 3) score += k;
                else score ++;
            }
            else{
                count = 0;
                score --;
            }
        }
        cout << score << endl;
    }
    return 0;
}